package com.tmall.tmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmall.tmall.mapper.MainCategoryMapper;
import com.tmall.tmall.pojo.Category;
import com.tmall.tmall.pojo.MainCategory;
import com.tmall.tmall.pojo.PageRt;
import com.tmall.tmall.service.MainCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MainCategoryServiceImpl implements MainCategoryService {
    @Autowired
    private MainCategoryMapper mainCategoryMapper;

    @Override
    public PageRt page(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<MainCategory> list = mainCategoryMapper.page(name);
        PageInfo<MainCategory> pageInfo = new PageInfo<>(list);
        return new PageRt(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public MainCategory getByName(MainCategory mainCategory) {
        return mainCategoryMapper.getByName(mainCategory.getName());
    }

    @Override
    public void insert(MainCategory mainCategory) {
        mainCategoryMapper.insert(mainCategory);
    }

    @Override
    public MainCategory getById(Integer id) {
        return mainCategoryMapper.getById(id);
    }

    @Override
    public void update(MainCategory mainCategory) {
        mainCategoryMapper.update(mainCategory);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        for (Integer id : ids) {
            MainCategory mainCategory = MainCategory.builder().id(id).deleteAt(LocalDateTime.now()).build();
            mainCategoryMapper.update(mainCategory);
        }
    }

    @Override
    public List<MainCategory> selectAll() {
        return mainCategoryMapper.selectAll();
    }
}
